<?php

class Smiley extends BaseOrm {
	public $Type = 'Smiley';

	public function Id() {
		return $this->Values['id'];
	}

	public function Width() {
		return NULL;
	}

	public function Height() {
		return NULL;
	}

	public function AddFavoriteUrl() {
		return Site::AddSmileyToFavoritesUrl($this->Values['id']);
	}

	public function RemoveFavoriteUrl() {
		return Site::RemoveSmileyFromFavoritesUrl($this->Values['id']);
	}

	public function CreatePostUrl() {
		return Site::CreateSmileyPostUrl($this->Values['id']);
	}

	public function IsFavorite() {
		return @$this->Values['favorite'];
	}

	public function Title() {
		return htmlspecialchars($this->Values['title']);
	}

	public function Description($formatted = true) {
		if ( !$formatted ) return htmlspecialchars($this->Values['description']);
		$b = trim($this->Values['description']);

		$b = str_replace("\r", '', $b);
		$b = str_replace("<br />", '', $b);
		$b = str_replace("<br/>", '', $b);
		$b = str_replace("<br>", '', $b);
		$b = str_replace("\n\n", '</p><p>', $b);
		$b = str_replace("\n", '<br/>', $b);
		$b = str_replace('&', '&amp;', $b);
		$b = '<p>' . $b . '</p>';

		return $b;
	}

	public function Href() {
		return Site::GetUrl('smiley/' . $this->Values['id'] . '/' . urlencode($this->Values['title']));
	}

	public function Url() {
		return Site::GetSmileyUrl($this->Values['path']);
	}

	public function EditUrl() {
		return '/smiley/edit/?smiley_id=' . $this->Values['id'];
	}

	public function SmileyUrl() {
		return '/smiley/' . $this->Values['id'];
	}

	public function AddFavorite($user) {
	
	}

	public function RemoveFavorite($user) {
		
	}

	private $_tags;
	public function Tags($offset = 0, $limit = 100) {
		if ( !$this->_tags ) $this->_tags = new TagList($offset, $limit, $this);
		return $this->_tags;
	}

	public function Edit($arguments) {
		$table_name = $this->TableName();

		$updates = array();

		if ( isset($arguments['title']) ) {
			$updates[] = "title=\"" . mysql_escape_string(trim($arguments['title'])) . "\"";
		}

		if ( isset($arguments['description']) ) {
			$updates[] = "description=\"" . mysql_escape_string(trim($arguments['description'])) . "\"";
		}

		if ( !count($updates) ) return false;

		$updates = implode(', ', $updates);

		$query = "
			/* " . __FILE__ . "::" . __LINE__ . " */
			UPDATE
				$table_name
			SET
				$updates
			WHERE
				id=" . $this->Values['id'] . "
			LIMIT 1
			";

		Dbi::GetResult($query);
	}
}

class SmileyList extends BaseOrmList {
	public $Type = 'Smiley';

	public function __construct($offset = 0, $limit = 10, $parent = NULL) {
		if ( $parent && $parent->Type == 'Tag' ) {
			$this->SetValuesByTag($parent, $offset, $limit);
		} elseif ( is_object($parent) ) {
			$this->SetValuesByParent($parent, $offset, $limit);
		}
	}

	// STATIC
	public function SetValuesByFavorites($user_id, $offset = 0, $limit = NULL) {
		$essential_columns = $this->GetEssentialColumns();
		$table_alias = $this->GetTableAlias();
		$table_name = $this->GetTableName();

		$count_query = "
			/* " . __FILE__ . "::" . __LINE__ . " */
			SELECT
				COUNT(*) AS c
			FROM
				msnu_smileyfavorite AS f
			WHERE
				f.usr_id=" . $user_id . "
			";

		$query = "
			/* " . __FILE__ . "::" . __LINE__ . " */
			SELECT
				" . $essential_columns . "
			FROM
				msnu_smileyfavorite AS f,
				$table_name AS $table_alias,
				msnu_usr AS u
			WHERE
				f.usr_id=" . $user_id . " AND
				f.smiley_id=$table_alias.id AND
				u.id=$table_alias.usr_id AND
				u.enabled=1 AND
				$table_alias.enabled=1
			ORDER BY
				sequence
					" . (($limit)?"
					LIMIT
						" . intval($offset) . ", " . intval($limit):"");

		return self::_set($offset, $limit, $query, $count_query);
	}

	public function SetValuesByTag($parent, $offset = 0, $limit = NULL) {
		$tag_id = $parent->Id();
		$essential_columns = $this->GetEssentialColumns();
		$table_alias = $this->GetTableAlias();
		$table_name = $this->GetTableName();

		$count_query = "
			/* " . __FILE__ . "::" . __LINE__ . " */
			SELECT
				COUNT(*) AS c
			FROM
				msnu_smileysgenre AS f
			WHERE
				f.sgenre_id=" . $tag_id . "
			";

		$query = "
			/* " . __FILE__ . "::" . __LINE__ . " */
			SELECT
				" . $essential_columns . ",
				COUNT(s.sgenre_id) AS tag_count
			FROM
				msnu_smileysgenre AS s,
				$table_name AS $table_alias
			LEFT OUTER JOIN
				msnu_usr AS u ON
					u.id = $table_alias.usr_id
			WHERE
				(
					u.enabled=1 OR
					u.id IS NULL
				) AND
				s.sgenre_id = " . $tag_id . " AND
				$table_alias.id=s.smiley_id
			GROUP BY
				s.smiley_id
			ORDER BY
				tag_count DESC
					" . (($limit)?"
					LIMIT
						" . intval($offset) . ", " . intval($limit):"");

		return self::_set($offset, $limit, $query, $count_query);
	}
}
